package com.szy.douyou_gateway.config;

import lombok.Data;

import java.io.Serializable;

/**
 * @author sunziyan
 * @date 2022/02/09
 */
@Data
public class ResultDTO<T> implements Serializable {
    private Boolean success = true;
    private Integer code;
    private T data;
    private String errorMessage;

    public ResultDTO<T> setFailed(String errorMessage) {
        this.code = 400;
        this.success = false;
        this.errorMessage = errorMessage;
        return this;
    }

    public ResultDTO<T> setFailed(int code, String errorMessage) {
        this.code = code;
        this.success = false;
        this.errorMessage = errorMessage;
        return this;
    }

    public static <T> ResultDTO<T> fail(String msg) {

        ResultDTO<T> r = new ResultDTO();
        r.setCode(400);
        r.setSuccess(Boolean.FALSE);
        r.setErrorMessage(msg);
        return r;
    }


    public static <T> ResultDTO<T> fail(int code, String msg) {
        ResultDTO<T> r = new ResultDTO();
        r.setCode(code);
        r.setSuccess(Boolean.FALSE);
        r.setErrorMessage(msg);
        return r;
    }


    public static <T> ResultDTO<T> success(T data) {
        ResultDTO<T> resultDTO = new ResultDTO();
        resultDTO.setCode(200);
        resultDTO.setData(data);
        return resultDTO;
    }

    public static <T> ResultDTO<T> success() {
        ResultDTO<T> resultDTO = new ResultDTO();
        resultDTO.setCode(200);
        return resultDTO;
    }

    @Override
    public String toString() {
        Object dataStr = this.data;
        if (this.data != null) {
            dataStr = this.data.toString();
        }

        return "ResultDTO{success=" + this.success + ", data=" + dataStr + ", errorCode=" + ", errorMessage='" + this.errorMessage + '\'' + '}';
    }


}
